package echat.future.cn.echat.my.view;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.netease.nim.avchatkit.activity.AVChatActivity;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.AppApplication;
import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityRechargeBinding;
import echat.future.cn.echat.my.adapter.RechargeAdapter;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.RechargeBean;
import echat.future.cn.echat.my.bean.Token;
import echat.future.cn.echat.my.bean.UserBean;
import echat.future.cn.echat.my.bean.WalletInfo;
import echat.future.cn.echat.my.bean.WxPayResult;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.StatusBarUtils;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;
import lumingweihua.future.cn.baselibgxh.base.MyApplication;
import lumingweihua.future.cn.baselibgxh.utils.MD5;

public class RechargeActivity extends BaseActivity<ActivityRechargeBinding>{

    public static int TYPE_ALIPAY = 1;
    public static int TYPE_WEIXIN = 2;
    private int curent_pay_typoe = TYPE_ALIPAY;

    RechargeAdapter mAdapter;

    List<RechargeBean> mDatas  = new ArrayList<>();

    private int type = 1;

    @Override
    public int layoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.hideStatus(this);
    }

    @Override
    public IBaseNavagation navagation() {
        return null;
    }

    @Override
    public void initView() {
        mAdapter = new RechargeAdapter(mDatas);
        bind.recyclerView.setLayoutManager(new GridLayoutManager(this,3));
        bind.recyclerView.setAdapter(mAdapter);
        bind.llWeixin.setOnClickListener(v->onChangePayType(v));
        bind.llAlipay.setOnClickListener(v->onChangePayType(v));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            RechargeBean lastBean;
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if(lastBean == mDatas.get(position)){
                    return;
                }
                if(lastBean != null){
                    lastBean.setCheck(false);
                }
                mDatas.get(position).setCheck(true);
                lastBean = mDatas.get(position);
                mAdapter.notifyDataSetChanged();
                if(curent_pay_typoe == TYPE_ALIPAY) {
                    alipay(lastBean);
                }else{
                    wechatPay(lastBean);
                }
            }
        });
        loadItems();
    }

    public void loadItems(){
        this.mDatas.add(new RechargeBean("￥50","50E币"));
        this.mDatas.add(new RechargeBean("￥100","100E币"));
        this.mDatas.add(new RechargeBean("￥200","200E币"));
        this.mDatas.add(new RechargeBean("￥500","500E币"));
        this.mDatas.add(new RechargeBean("￥1000","1000E币"));
        this.mDatas.add(new RechargeBean("￥2000","2000E币"));
        this.mDatas.add(new RechargeBean("￥5000","5000E币"));
        this.mDatas.add(new RechargeBean("￥10000","10000E币"));
        this.mAdapter.notifyDataSetChanged();
    }


    /**
     * 切换支付方式
     */
    public void onChangePayType(View view){
        if(view.getId() == R.id.ll_alipay){
            curent_pay_typoe = TYPE_ALIPAY;
            bind.llAlipay.setBackground(getResources().getDrawable(R.drawable.bg_recharge_alipay));
            bind.llWeixin.setBackgroundColor(Color.TRANSPARENT);
            bind.ivAlipayCheck.setVisibility(View.VISIBLE);
            bind.ivWeixinCheck.setVisibility(View.GONE);
        }else{
            curent_pay_typoe = TYPE_WEIXIN;
            bind.llWeixin.setBackground(getResources().getDrawable(R.drawable.bg_recharge_weixin));
            bind.llAlipay.setBackgroundColor(Color.TRANSPARENT);
            bind.ivAlipayCheck.setVisibility(View.GONE);
            bind.ivWeixinCheck.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 获取充值订单号
     * @param rechargeBean
     */
    private void alipay(RechargeBean rechargeBean){
        Intent intent = new Intent();
//    Intent intent = new Intent(Intent.ACTION_VIEW,uri);
        intent.setAction("android.intent.action.VIEW");
//        Uri content_url = Uri.parse(String.format("https://eliao-tv.com/api/user/alipay_recharge?member_id=%s&money=%s",UserHelper.getUserId(),rechargeBean.getValue().substring(1)));
        Uri content_url = Uri.parse(String.format("https://eliao-tv.com/api/user/alipay_recharge?member_id=%s&money=1",UserHelper.getUserId(),rechargeBean.getValue().substring(1)));
        intent.setData(content_url);
        startActivity(intent);

    }

    private void wechatPay(RechargeBean rechargeBean){
        new HttpUtilsRequst<WxPayResult>(this,
                HttpUtilsRequst.retrofitClient.create(UserApi.class).wxRechage(UserHelper.getUserId(),
                        rechargeBean.getValue().substring(1)))
                .setmOnRequstSuccessListener(new HttpUtilsRequst.OnRequstSuccessListener<WxPayResult>() {
                    @Override
                    public void onSuccss(WxPayResult wxPayResult) {
                        Intent intent = new Intent(RechargeActivity.this,WxPayActivity.class);
                        intent.putExtra("url",String.format(wxPayResult.getMweb_url()));
                        intent.putExtra("orderNumber",String.format(wxPayResult.getOut_trade_no()));
                        startActivity(intent);
                    }
                }).start();

    }


    WalletInfo mwalletInfo;
    private void loadData(){
        new HttpUtilsRequst<>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).getMoney(UserHelper.getToken()))
                .setmOnRequstSuccessListener(new HttpUtilsRequst.OnRequstSuccessListener<WalletInfo>() {
                    @Override
                    public void onSuccss(WalletInfo o) {
                        mwalletInfo = o;
                        bind.coinNumber.setText(mwalletInfo.getWallet()+"");
                    }
                })
                .start();

        String key = "9b4aaf?&&##664bdef@c3fc3efa9";
        new HttpUtilsRequst<Token>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).getUserInfo(UserHelper.getToken(), MD5.Md5(System.currentTimeMillis()/1000+key).toLowerCase()),false)
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<UserBean>) userInfo -> {
                    MyApplication.getInstance().e_money = userInfo.getE_money();
                }).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(AppApplication.isGotoCharge){
            AppApplication.isGotoCharge = false;
            startActivity(new Intent(this, AVChatActivity.class));
        }
    }
}
